<?php

require_once APPPATH . "/controllers/BasePriv.php";

class Down_excel extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_statics", "table");
        $this->load->library('Excel', '', 'excel');
        // Session 类现在可以通过下面的方式访问:
    }

    /**
     * 前端基础数据
     */
    public function index()
    {
        $this->layout("statics/excel");
    }

    /**
     * 配音EXCEL
     */
    public function audio()
    {
        $startDate = $_GET['start_date'];
        $endDate = $_GET['end_date'];
        $type = $_GET['type'];
        $where['startDate'] = $startDate . ' 00:00:00';
        $where['endDate'] = $endDate . ' 23:29:29';

        $datetime_start = new DateTime($startDate);
        $datetime_end = new DateTime($endDate);
        $obj = $datetime_start->diff($datetime_end);
        if ($obj->days > 6) {
            die("日期最多选择七天");
        }

        $data = [];
        $content = [];
        switch ($type) {
            case 1:
                // PV OK
                $typeName = 'PV';
                $data = $this->table->pv_audio($where);
                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', 'PV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count']];
                }
                break;
            case 2:
                // UV OK
                $typeName = 'UV';
                $data = $this->table->uv_audio($where);
                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', 'UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count']];
                }
                break;
            case 3:
                // 分享数 OK
                $typeName = '分享数';
                $data = $this->table->share_audio($where);
                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', '分享数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count']];
                }
                break;
            case 4:
                // 产出比 OK
                // echo '<pre>';
                $typeName = '产出比';
                $data = $this->table->ratio_audio($where);
                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', '总数','合成数','占比'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count'],$v['successCount'],$v['ratio']];
                }
                break;
            case 5:
                // 配音条数 OK
                $typeName = '配音条数';
                $data = $this->table->use_count_audio($where);
                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', '配音条数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count']];
                }
                break;

            case 6:
                // PV/UV OK
                $typeName = 'PV/UV';
                $data = $this->table->pv_audio($where);
                $uvs = $this->table->uv_audio($where);
                $uvs_re = [];
                foreach ($uvs as $k => $v) {
                    $uvs_re[$v['video_id']] = $v;
                }

                $title = ['video_id', '分类', '话题', '标题', '后台已挑(3为已挑)', 'PV','UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['video_id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['video_type'], $v['count'],$uvs_re[$v['video_id']]['count']];
                }
                break;

            default:
                break;
        }
        $this->excel->setDownFileName("配音[$typeName][$startDate/$endDate]");
        $this->excel->setActiveSheetIndex(0,  "配音（" . $startDate . '~' . $endDate . ")");
        $this->excel->createExcel($title, $content);
    }

    /**
     * 视频 EXCEL
     */
    public function video()
    {
        // echo "<pre>";

        $startDate = $_GET['start_date'];
        $endDate = $_GET['end_date'];
        $type = $_GET['type'];
        $where['startDate'] = $startDate . ' 00:00:00';
        $where['endDate'] = $endDate . ' 23:29:29';

        $datetime_start = new DateTime($startDate);
        $datetime_end = new DateTime($endDate);
        $obj = $datetime_start->diff($datetime_end);
        if ($obj->days > 6) {
            die("日期最多选择七天");
        }

        $data = [];
        $content = [];
        $title = [];
        $typeName = '';
        switch ($type) {
            case 1:
                // PV OK
                $typeName = 'PV';
                $data = $this->table->pv_video($where, 2);
                $title = ['id', '分类', '话题', '标题', 'PV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 2:
                // UV OK
                $typeName = 'UV';
                $data = $this->table->uv_video($where,2);
                $title = ['id', '分类', '话题', '标题', 'UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 3:
                // 点赞数 OK
                $typeName = '点赞数';
                $data = $this->table->zan_video($where, 2);
                $title = ['video_id', '分类', '话题', '标题', '点赞数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 4:
                // 分享数 OK
                $typeName = '分享数';
                $data = $this->table->share_video($where, 2);
                $title = ['video_id', '分类', '话题', '标题', '分享数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 6:
                // PV/UV OK
                $typeName = 'PV/UV';
                $data = $this->table->pv_video($where,2);
                $uvs = $this->table->uv_video($where,2);
                $uvs_re = [];
                foreach ($uvs as $k => $v) {
                    $uvs_re[$v['id']] = $v;
                }

                $title = ['id', '分类', '话题', '标题', 'PV','UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count'],$uvs_re[$v['id']]['count']];
                }
                break;
            default:
                die("类型错误");
                break;
        }
        $this->excel->setDownFileName("视频[$typeName][$startDate/$endDate]");
        $this->excel->setActiveSheetIndex(0,  "视频（" . $startDate . '~' . $endDate . ")");
        $this->excel->createExcel($title, $content);
    }

    /**
     * UGC EXCEL
     */
    public function ugc()
    {
        $startDate = $_GET['start_date'];
        $endDate = $_GET['end_date'];
        $type = $_GET['type'];
        $where['startDate'] = $startDate . ' 00:00:00';
        $where['endDate'] = $endDate . ' 23:29:29';

        $datetime_start = new DateTime($startDate);
        $datetime_end = new DateTime($endDate);
        $obj = $datetime_start->diff($datetime_end);
        if ($obj->days > 6) {
            die("日期最多选择七天");
        }

        $data = [];
        $content = [];
        $title = [];
        $typeName='';
        switch ($type) {
            case 1:
                // PV OK
                $typeName = 'PV';
                $data = $this->table->pv_video($where, 1);
                $title = ['id', '分类', '话题', '标题', 'PV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 2:
                // UV OK
                $typeName = 'UV';
                $data = $this->table->uv_video($where, 1);
                $title = ['id', '分类', '话题', '标题', 'UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 3:
                // 点赞数 OK
                $typeName = '点赞数';
                $data = $this->table->zan_video($where, 1);
                $title = ['video_id', '分类', '话题', '标题', '点赞数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 4:
                // 分享数 OK
                $typeName = '分享数';
                $data = $this->table->share_video($where, 1);
                $title = ['video_id', '分类', '话题', '标题', '分享数'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count']];
                }
                break;
            case 6:
                // PV/UV OK
                $typeName = 'PV/UV';
                $data = $this->table->pv_video($where,1);
                $uvs = $this->table->uv_video($where,1);
                $uvs_re = [];
                foreach ($uvs as $k => $v) {
                    $uvs_re[$v['id']] = $v;
                }

                $title = ['id', '分类', '话题', '标题', 'PV','UV'];
                $content = [];
                foreach ($data as $k => $v) {
                    $content[] = [$v['id'], $v['type_name'], $v['sub_name'], $v['video_desc'], $v['count'],$uvs_re[$v['id']]['count']];
                }
                break;
            default:
                die("类型错误");
                break;
        }
        $this->excel->setDownFileName("UGC视频[$typeName][$startDate/$endDate]");
        $this->excel->setActiveSheetIndex(0, "UGC视频（" . $startDate . '~' . $endDate . ")");
        $this->excel->createExcel($title, $content);
    }
}